package com.example.service;


import cn.hutool.core.date.DateUtil;
import com.example.entity.Account;
import com.example.entity.Book;
import com.example.exception.CustomerException;
import com.example.mapper.BookMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public List<Book> selectAll(Book book){
        Account currentUser = TokenUtils.getCurrentUser();
        if("USER".equals(currentUser.getRole())){
            book.setGrounding(true);//普通用户只能看到上架的
        }
        return bookMapper.selectAll(book);
    }

    public PageInfo<Book> selectPage(Integer pageNum, Integer pageSize,Book book) {
        Account currentUser = TokenUtils.getCurrentUser();
        if("USER".equals(currentUser.getRole())){
            book.setGrounding(true);//普通用户只能看到上架的
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Book> list = bookMapper.selectAll(book);
        return PageInfo.of(list);

    }

    public void add(Book book) {
        bookMapper.insert(book);
    }

    public void update(Book book) {
        bookMapper.updateById(book);
    }

    public void deleteById(Integer id) {
        bookMapper.deleteById(id);
    }

    public Book selectByCategoryId(Integer id){
      Book book= bookMapper.selectByCategoryId(id);
      return book;
    }

    public Book selectById(Integer id) {
        Book book=bookMapper.selectById(id);
        return book;
    }


//    public List<Book> selectRank() {
//        bookMapper.selectRank();
//    }
}
